package com.aliyun.messagemgt.repository;

import com.aliyun.messagemgt.common.annotation.PagingAnnotation;
import com.aliyun.messagemgt.converter.BaseMessageSourceConverter;
import com.aliyun.messagemgt.domain.repository.BaseMessageSourceRepository;
import com.aliyun.messagemgt.dto.BaseMessageSourceDto;
import com.aliyun.messagemgt.dto.BassMessageSourceDto;
import com.aliyun.messagemgt.dto.base.BaseMessageSourceDTO;
import com.aliyun.messagemgt.dto.query.BaseMessageSourceQuery;
import com.aliyun.messagemgt.entities.BaseMessageSourceEntity;
import com.aliyun.messagemgt.repository.mappers.BaseMessageSourceMapper;
import com.aliyun.messagemgt.repository.mappers.BaseTenantUserMapper;
import com.aliyun.messagemgt.repository.mappers.MessageRecordMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangshaobo
 * @date 2021年10月14日
 */
@Service
public class BaseMessageSourceMybatisRepository implements BaseMessageSourceRepository {

    @Autowired
    private BaseMessageSourceMapper baseMessageSourceMapper;

    @Autowired
    private MessageRecordMapper messageRecordMapper;

    @Autowired
    private BaseTenantUserMapper baseTenantUserMapper;

    @Override
    @PagingAnnotation
    public Object listBaseMsgSource(BaseMessageSourceQuery query) {

        QueryWrapper<BaseMessageSourceEntity> queryWrapper = new QueryWrapper<>();
//        if (query.getTenantCode() != null){
//            queryWrapper.eq("tenant_code",query.getTenantCode());
//        }
        if (query.getSourceCode() != null) {
            queryWrapper.eq("source_code", query.getSourceCode());
        }
        if (query.getSourceName() != null) {
            queryWrapper.eq("source_name", query.getSourceName());
        }
        if (query.getSceneCode() != null) {
            queryWrapper.eq("scene_code", query.getSceneCode());
        }
        if (query.getSceneName() != null) {
            queryWrapper.eq("sceneName", query.getSceneName());
        }
        if (query.getIsEnabled() != null) {
            queryWrapper.eq("is_enabled", query.getIsEnabled());
        }
        if (StringUtils.isNotEmpty(query.getSourceNameOrSourceCode())) {
            queryWrapper.like("source_name", query.getSourceNameOrSourceCode()).or().like("source_code", query.getSourceNameOrSourceCode());
        }
        if (StringUtils.isNotEmpty(query.getSceneCodeOrSceneName())) {
            queryWrapper.like("scene_code", query.getSceneCodeOrSceneName()).or().like("scene_name", query.getSceneCodeOrSceneName());
        }
        if (query.getTenantCodeList() != null && query.getTenantCodeList().size() > 0) {
            queryWrapper.in("tenant_code", query.getTenantCodeList());
        }
        queryWrapper.eq("is_deleted", "N");
        List<BaseMessageSourceEntity> entities = baseMessageSourceMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(entities)) {
            return new PageInfo<>();
        }
        List<BassMessageSourceDto> bassMessageSources = dataConvert(entities);
        //查询租户编码
        List<BassMessageSourceDto> bassMessageSourceDtoList = new ArrayList<>();
        for (BassMessageSourceDto bassMessageSourceDto : bassMessageSources) {
            if (bassMessageSourceDto.getTenantCode() != null) {
                bassMessageSourceDto.setTenantName(baseTenantUserMapper.queryTenantName(bassMessageSourceDto.getTenantCode()));
            }
            bassMessageSourceDtoList.add(bassMessageSourceDto);
        }

        PageInfo pageInfo = new PageInfo(bassMessageSourceDtoList);
        pageInfo.setList(bassMessageSourceDtoList);
        return pageInfo;
    }

    @Override
    public Object isEnabled(BaseMessageSourceDto baseMessageSourceDto) {
        BaseMessageSourceEntity sourceEntity = new BaseMessageSourceEntity();
        BeanUtils.copyProperties(baseMessageSourceDto, sourceEntity);
        return baseMessageSourceMapper.updateById(sourceEntity);
    }

    @Override
    public Object queryLog(BaseMessageSourceQuery messageSourceQuery) {
        return messageRecordMapper.selectBySceneCode(messageSourceQuery);
    }


    /**
     * 实体类转换
     *
     * @param list 集合
     * @return BassMessageSourceDto
     */
    private List<BassMessageSourceDto> dataConvert(List<BaseMessageSourceEntity> list) {
        if (list == null || list.size() <= 0) {
            return null;
        }
        List<BassMessageSourceDto> data = new ArrayList<>();
        for (BaseMessageSourceEntity baseMessageSourceEntity : list) {
            BassMessageSourceDto sourceDto = new BassMessageSourceDto();
            BeanUtils.copyProperties(baseMessageSourceEntity, sourceDto);
            data.add(sourceDto);
        }
        return data;
    }

    @Override
    public BaseMessageSourceDTO getByTenantCodeAndSceneCode(String tenantCode, String sceneCode) {
        QueryWrapper<BaseMessageSourceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant_code", tenantCode);
        queryWrapper.eq("scene_code", sceneCode);
        queryWrapper.eq("is_enabled", "Y");
        queryWrapper.eq("is_deleted", "N");
        BaseMessageSourceEntity baseMessageSourceEntity = baseMessageSourceMapper.selectOne(queryWrapper);
        BaseMessageSourceDTO baseMessageSourceDTO = BaseMessageSourceConverter.INSTANCE.baseMessageSourceEntity2baseMessageSourceDTO(baseMessageSourceEntity);
        return baseMessageSourceDTO;
    }
}
